package com.itheima.reggie.utils;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.itheima.reggie.common.CustomException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * 短信发送工具类
 */
@Slf4j
public class SMSUtils {

	private static final String accountSid = "8a216da87de15752017e0393329807f6";
	private static final String accountToken = "10cc7d673a184c949e15c6e0affb70d4";
	private static final String appId = "8aaf0708809721d00180d6d99dd81278";

	private static CCPRestSmsSDK sdk;

	static {
		sdk = new CCPRestSmsSDK();
		sdk.init("app.cloopen.com", "8883");
		sdk.setAccount(accountSid, accountToken);
		sdk.setAppId(appId);
	}

	/**
	 * 发送短信
	 *
	 * @param phoneNumbers 手机号
	 * @param param        验证码
	 */
	public static void sendMessage(String phoneNumbers, String param) {
		// 发送验证码
		String[] datas = {param, "30"}; // 你的验证码是 {} , 请在 {} 分钟之内使用
		HashMap<String, Object> result = sdk.sendTemplateSMS(phoneNumbers, "1", datas);
		System.out.println("result: " + result);
		if (result.get("statusCode").equals("000000")) {
			log.info("短信发送成功");
		} else {
			throw new CustomException("短信发送失败");
		}
	}

}
